/**   
* @Title: UserService.java 
* @Package com.csis.system.service 
* @Description: TODO
* @author dailey  
* @date 2012-9-27
* @version V1.0   
*/
package com.csis.system.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.csis.framework.db.BaseDao;
import com.csis.framework.domain.Condition;
import com.csis.framework.util.ServiceLocator;
import com.csis.security.domain.Role;
import com.csis.security.domain.RoleUserMap;
import com.csis.security.service.RoleService;
import com.csis.system.common.IStaticData;
import com.csis.system.common.convert.Converter.ConvertArray;
import com.csis.system.domain.User;
import com.csis.system.domain.UserFormBean;

/**
 * @author dailey
 *
 */
@Service
public class UserService {
	private Log log = LogFactory.getLog(getClass());

	@Resource
	protected BaseDao baseDao;

	public User loadUser(String userName) {
		String sql = "SELECT * FROM " + IStaticData.TABLE_USER + " WHERE " + IStaticData.COLUMN_USER_USERNAME + " = ?";
		List<User> userList = baseDao.query4SingleTable(User.class, sql, userName);
		if (userList.size() == 1) {
			return userList.get(0);
		}
		return null;
	}

	public User load(Integer id) {
		return baseDao.load(User.class, id);
	}

	@Transactional
	public void saveUser(User user) {
		log.info("UserService>>SaveUser>>" + user);
		if (user.getId() == null) {
			baseDao.persist(user);
		} else {
			baseDao.merge(user);
		}
	}

	@Transactional
	public void removeUser(User user) {
		log.info("UserService>>RemoveUser" + user);
		baseDao.remove(User.class, user.getId());
	}

	@Transactional
	public void remove(ConvertArray<Integer> ids) {
		if (ids == null)
			return;
		for (Integer id : ids) {
			baseDao.remove(User.class, id);
		}
	}

	public UserFormBean list(UserFormBean formBean) {
		String sql = getSearchSql(formBean);

		Condition condition = formBean.getCondition();
		if (condition != null) {
			sql = condition.getCondition(sql);
		}
		// importance
		int maxCount = getMaxCount(formBean);
		formBean.getPage().setMaxCount(maxCount);
		sql = formBean.getPage().perPageSql(sql);
		List<User> beans = baseDao.query4SingleTable(User.class, sql);
		formBean.setBeans(beans);
		return formBean;
	}

	/**
	 * @return
	 */
	public int getMaxCount(UserFormBean formBean) {
		String sql = getSearchSql(formBean);
		return baseDao.getMaxCount(sql);
	}

	/**
	 * @param formBean
	 * @return
	 */
	public String getSearchSql(UserFormBean formBean) {
		String sql = "SELECT user_id,user_name,user_phone,user_address,user_note,user_password FROM "
				+ IStaticData.TABLE_USER + " WHERE 1=1 ";
		return sql;
	}

	@Transactional
	public void assignRole(User user, ConvertArray<Integer> ids) {
		if (ids == null)
			return;
		RoleUserMapService roleUserMapService = ServiceLocator.getInstance().getService(RoleUserMapService.class);
		RoleService roleService = ServiceLocator.getInstance().getService(RoleService.class);
		roleUserMapService.remove(user);
		for (Integer id : ids) {
			Role role = roleService.load(id);
			if (role == null)
				continue;
			RoleUserMap roleUserMap = new RoleUserMap(user.getId(), role.getId());
			roleUserMapService.save(roleUserMap);
		}
	}

	/**
	 * @param name
	 * @return
	 */
	public List<User> getUsersByName(String name) {
		return baseDao.query4SingleTable(User.class, "SELECT * FROM " + IStaticData.TABLE_USER + " WHERE "
				+ IStaticData.COLUMN_USER_USERNAME + " = ?", name);
	}

}
